package org.example.codeservce.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import org.example.codeservce.contr.PayEnum;
import org.example.codeservce.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author Li Qing
 * date: 2024/7/18 10:48
 * description:
 */
@RestController
@Api("接口")
@RequestMapping("callback")
public class CallbackController {

    @Resource
    private OrderService orderService;

    @SneakyThrows
    @GetMapping
    public void callback(Map<String, Object> map, HttpServletResponse response) {
        JSONObject parsed = JSONUtil.parseObj(map);
        String tradeStatus = parsed.getStr("trade_status");
        if (PayEnum.TRADE_SUCCESS.name().equals(tradeStatus)) {
            BigDecimal parsedBigDecimal = parsed.getBigDecimal("receipt_amount");
            String outTradeNo = parsed.getStr("out_trade_no");
            orderService.callback(parsedBigDecimal,outTradeNo);
        }
        response.getWriter().print("success");
    }
}
